package com.FengChaoSystem.tool;

import com.Mybatis.executor.BaseExecutor;
import com.Mybatis.session.defaults.DefaultSqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * 用于执行定时任务的线程池
 */
public class ThreadPool {
    private static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
    private static Map<String, ScheduledFuture<?>>futureMap=new HashMap<>();

    private static Logger logger =LoggerFactory.getLogger (ThreadPool.class);
    public static void addTask(String email,String onlyId,int userId){
        TimeTask timeTask=new TimeTask();
        timeTask.setEmail(email);
        timeTask.setOnlyId(onlyId);
        timeTask.setUserId(userId);
        logger.info("开始增加定时任务了");
        ScheduledFuture<?> scheduledFuture = scheduler.schedule(timeTask, 120,  TimeUnit.SECONDS);
        logger.info("发送了滞留通知，对应的onlyId为："+onlyId);
        futureMap.put(onlyId,scheduledFuture);

    }
    public static void removeTask(String onlyId){
        ScheduledFuture<?> scheduledFuture = futureMap.get(onlyId);
        if(scheduledFuture!=null)
        scheduledFuture.cancel(false);
        futureMap.remove(onlyId);
        logger.info("成功取消onlyId为："+onlyId+"的定时任务");
    }




}
